package sy.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/**
 * Created by Administrator on 2016/6/27.
 */
public class SendMessage {

    public static String remoteJsonRequest(String url, int timeout, JSONObject object) {
        URL connect;
        StringBuffer data = new StringBuffer();
        try {
            connect = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setReadTimeout(timeout);
            connection.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            paramout.write(object.toString());
            paramout.flush();
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader reader = new BufferedReader(inputStreamReader);

            String line;
            while ((line = reader.readLine()) != null) {
                data.append(line);
            }
            paramout.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data.toString();
    }
    public static String sendFlag(String telephone,String verificatCode){
    	long timestrap = System.currentTimeMillis(); 
    	String key = "619szzhlyZhly619"+String.valueOf(timestrap);
    	String keyMD5_32 = Encrypt.md5(key);
    	String content = "您当前的验证码为:"+verificatCode+",有效期为5分钟";
    	String url = "http://client.sms10000.com/api/webservice?"+"cmd=send&eprId=619&userId=szzhly&key="+
    	    keyMD5_32+"&timestamp="+timestrap+"&format=1&mobile="+telephone+"&msgId=323&content="+content;
    	String result = SendMessage.remoteJsonRequest(url, 5000, new JSONObject());
    	JSONObject obj = JSONObject.parseObject(result);
    	return obj.getString("result").toString();
    }
    public static void main(String[] args){
    }

}
